<?php
/**
 * @file $HeadURL: tryproduct.php $
 * @author $Author: LiangJian (liangjian@haibao.com) $
 * @date $Date: 2015-3-21 上午11:13:23 $
 * @brief 试用中心业务层
 */
namespace haibao\cms\business;

class TryProduct extends \haibao\cms\business\Base{
	
	/**
	 * 根据试用产品ID查询详细信息
	 */
	public static function getTryProductById($tryId){
		if(!$tryId){
			throw new BusinessException('试用产品ID传递错误');
		}
		$data = new \haibao\cms\data\TryProduct();
		$result = $data->getTryProductById($tryId);
		
		// 根据试用产品ID查询试用产品详细内容
		if($result){
			$result->Content = self::getTryContetById($tryId);
		}
		
		return $result;
	}
	
	public static function getTryProductByIds($tryIds){
	    $data = new \haibao\cms\data\TryProduct();
	    return  $data->getTryProductByIds($tryIds);
	}
	
	/**
	 * 获取试用产品列表
	 */
	public static function getTryProductList(){
	
	    $data = new \haibao\cms\data\TryProduct();
	    
	    return $data->getTryProductList();
	    
	}
	/**
	 * 获取其他正在开放试用的产品
	 * @param 当前免费试用 $tryId
	 */
	public static function getTryProductBesidesById($tryId){
	    if(!$tryId){
	        throw new BusinessException('试用产品ID传递错误');
	    }
	    $data = new \haibao\cms\data\TryProduct();
	    $result = $data->getTryProductBesidesById($tryId);
	    return $result;
	}
	/**
	 * 试用产品
	 */
	public static function getTryProductByQuery($query){
	
		if(!$query){
			$query = new \haibao\cms\model\filter\TryProduct();
		}
		$data = new \haibao\cms\data\TryProduct();
		$tryList = $data->getTryProductByQuery($query);
	
		return $tryList;
	}
	
	/**
	 * 更改试用产品状态
	 */
	public static function updateTryProductStatus($tryIds, $published){
		if( !isset($tryIds) ) {
			throw new BusinessException('试用产品ID传递错误');
		}
		if( !isset($published) ) {
			throw new BusinessException('状态错误');
		}
		$data = new \haibao\cms\data\TryProduct();
		$data->updateTryProductStatus($tryIds, $published);
		
		self::saveOperateHistory(self::FUNC_CODE_TRYOUT_UPDATE_STATUS, array($tryIds, $published));
	}

	/**
	 * 获取申请列表
	 */
	public static function getApplyByQuery($query){
		if(!$query){
			$query = new \haibao\cms\model\filter\TryProduct();
		}
		$data = new \haibao\cms\data\TryProduct();
		$applyList = $data->getApplyByQuery($query);
	}
	
	/**
	 * 根据试用产品ID（array）查询产品申请数量
	 */
	public static function getApplyNumByIds($tryIds){
		$data = new \haibao\cms\data\TryProduct();
		return $data->getApplyNumByIds($tryIds);
	}
	
	/**
	 * 保存试用产品
	 * @param \haibao\cms\model\data\TryProduct $model
	 */
	public static function addTryProduct($model){
		self::checkOperation(self::FUNC_CODE_TRYOUT_ADD);
		
		self::checkForm($model);
		
		$data = new \haibao\cms\data\TryProduct();
		$data->addTryProduct($model);
		
		self::saveOperateHistory(self::FUNC_CODE_TRYOUT_ADD, $model->toArray(true,true));
	}
	
	/**
	 * 更新试用产品
	 */
	public static function editTryProduct($model){
		self::checkOperation(self::FUNC_CODE_TRYOUT_UPDATE);
		
		self::checkForm($model);
		
		$data = new \haibao\cms\data\TryProduct();
		$data->updateTryProductById($model); 
		
		self::saveOperateHistory(self::FUNC_CODE_TRYOUT_UPDATE, $model->toArray(true,true));
	}
	
	/**
	 * 删除试用产品
	 */
	public static function deleteTryProduct($tryId){
		self::checkOperation(self::FUNC_CODE_TRYOUT_DELETE);
		
		if(!$tryId){
			throw new BusinessException("试用产品ID不能为空");
		}
		$data = new \haibao\cms\data\TryProduct();
		$data->deleteTryProductById($tryId, self::getCurrentUser()->Id);
		
		self::saveOperateHistory(self::FUNC_CODE_TRYOUT_DELETE, $tryId);
	}
	
	/**
	 * 根据试用产品ID查询详细信息
	 * @param unknown $tryId
	 */
	public static function getTryContetById($tryId){
		if(!$tryId){
			return '';
		}
		
		$data = new \haibao\cms\data\TryProduct();
		return $data->getTryContetById($tryId);
	}
	
	/**
	 * 合作品牌
	 */
	public static function getTryBrandList(){
		$data = new \haibao\cms\data\TryProduct();
		return $data->getTryBrandList();
	}
	
	/**
	 * 根据品牌id查询相关试用产品
	 */
	public static function getProductByBrand($brandId, $limit=20){
        $data = new \haibao\cms\data\TryProduct();
        return $data->getProductByBrand($brandId, $limit);
	}
	
	/**
	 * @param \haibao\cms\model\data\TryProduct $model
	 * @throws BusinessException
	 */
	private static function checkForm($model){
		if(!$model->Title){
			throw new BusinessException('试用产品标题不能为空');
		}
		if(!$model->Introduce){
			throw new BusinessException('试用产品简要介绍不能为空');
		}
		if(!$model->Amount){
			throw new BusinessException('产品数量不能为空');
		}
		if(!$model->PublishDate){
			throw new BusinessException('发布时间不能为空');
		}
		if(!$model->ApplyStartTime){
			throw new BusinessException('试用申请开始时间不能为空');
		}
		if(!$model->ApplyEndTime){
			throw new BusinessException('试用申请结束时间不能为空');
		}
		if($model->ApplyStartTime > $model->ApplyEndTime){
			throw new BusinessException('试用申请开始时间不能大于结束时间');
		}
		if(!$model->ExperienceStartTime){
			throw new BusinessException('试用心得提交开始时间不能为空');
		}
		if(!$model->ExperienceEndTime){
			throw new BusinessException('试用心得提交结束时间不能为空');
		}
		if($model->ExperienceStartTime > $model->ExperienceEndTime){
			throw new BusinessException('试用心得提交开始时间不能大于结束时间');
		}
		if(!$model->PicId){
		    throw new BusinessException('请上传产品图片');
		}
		
		$model->Amount = (int)$model->Amount;
		$model->Price = sprintf("%.2f", $model->Price);
		// 该产品显示状态
		$model->Status = \haibao\cms\model\data\TryProduct::STATUS_ENABLE;
		// 该产品发布状态
		$model->Published = $model->PublishDate < date('Y-m-d H:i:s') ? $model->Published = \haibao\cms\model\data\TryProduct::PUBLISHED_YES : $model->Published = \haibao\cms\model\data\TryProduct::PUBLISHED_PRE;
		$model->LastModifyTime = new \DateTime();
		$model->LastModifyUser = self::getCurrentUser()->Id;
	}
	
	
}